package com.xiaowei.gym.gymjsp.ware.dao.impl;

import com.mysql.cj.util.StringUtils;
import com.xiaowei.gym.gymjsp.ware.dao.SalaryDao;
import com.xiaowei.gym.gymjsp.ware.entity.AdminEntity;
import com.xiaowei.gym.gymjsp.ware.entity.SalaryEntity;
import com.xiaowei.gym.gymjsp.ware.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SalaryDaoImpl implements SalaryDao {
    @Override
    public int createSalary(SalaryEntity salaryEntity) {
        int i = 0;
        String sql = "INSERT INTO gym_salary(employee_id,month,amount) VALUES(?,?,?)";
        Object[] params = {salaryEntity.getEmployee_id(),salaryEntity.getMonth(),salaryEntity.getAmount()};
        try {
            i = JdbcUtil.executeUpdate(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return i;
    }

    @Override
    public int getSalaryCount() {
        String sql = "SELECT count(1) from gym_salary";
        Integer i = 0;
        try {
            i = JdbcUtil.executeQuery(sql, rs -> {
                int count = 0;
                if (rs.next()) {
                    count = rs.getInt("count(1)");
                }
                return count;
            }, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return i;
    }

    @Override
    public List<SalaryEntity> getSalaryList(String queryUserName, int currentPageNo, int pageSize) {
        List<SalaryEntity> salaryEntities = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from gym_salary ");
        List<Object> list = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(queryUserName)){
            sql.append("where employee_id = ?");
            list.add("queryUserName+");
        }
        //在mysql数据库中，分页使用 limit startIndex，pageSize ; 总数
        sql.append(" order by month DESC limit ?,?");
        currentPageNo = (currentPageNo - 1) * pageSize;
        list.add(currentPageNo);
        list.add(pageSize);
        Object[] params = list.toArray();
        System.out.println("sql ----> " + sql.toString());
        try {
            List<SalaryEntity> salaryEntityList = new ArrayList<>();
            salaryEntities = JdbcUtil.executeQuery(sql.toString(), rs -> {
                while (rs.next()) {
                    SalaryEntity salaryEntity = new SalaryEntity();
                    salaryEntity.setId(rs.getInt("id"));
                    salaryEntity.setEmployee_id(rs.getInt("employee_id"));
                    salaryEntity.setMonth(rs.getTimestamp("month"));
                    salaryEntity.setAmount(rs.getBigDecimal("amount"));
                   salaryEntityList.add(salaryEntity);
                }
                return salaryEntityList;
            }, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return salaryEntities;
    }

    @Override
    public SalaryEntity getSalaryById(Integer id) {
        String sql = "SELECT id,employee_id,month,amount FROM gym_salary WHERE id = ?";
        SalaryEntity result = null;
        try {
            result = JdbcUtil.executeQuery(sql, rs -> {
                SalaryEntity salaryEntity = new SalaryEntity();
                if (rs.next()) {
                    salaryEntity.setId(rs.getInt("id"));
                    salaryEntity.setEmployee_id(rs.getInt("employee_id"));
                    salaryEntity.setMonth(rs.getTimestamp("month"));
                    salaryEntity.setAmount(rs.getBigDecimal("amount"));
                }
                return salaryEntity;
            }, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer modify(SalaryEntity salaryEntity) {
        String sql = "UPDATE gym_salary SET month = ?,amount = ? WHERE id = ?";
        Object[] params = {salaryEntity.getMonth(),salaryEntity.getAmount(),salaryEntity.getId()};
        int i = 0;
        try {
            i = JdbcUtil.executeUpdate(sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public int[] deleteSalaryByIds(Integer[] delId) {
        String sql = "DELETE FROM gym_salary WHERE id = ?";
        Object[] params = delId;
        System.out.println(params);
        int i[] = null;
        try {
            i = JdbcUtil.batchDelete(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }
}
